//
//  DHOrderDetailLeaseCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/16.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHOrderDetailLeaseCell.h"

#pragma mark - DHOrderDetailLeaseCell_ContentCell
@interface DHOrderDetailLeaseCell_ContentCell : DHTableViewCell

@property (strong, nonatomic) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *numberLabel;
@property (strong, nonatomic) UILabel *priceLabel;

- (void)updateCell:(NSDictionary *)product;

@end

@implementation DHOrderDetailLeaseCell_ContentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        if (!self.nameLabel) {
            self.nameLabel = [[UILabel alloc] init];
            self.nameLabel.textColor = [UIColor blackColor];
            self.nameLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
        }
        if (!self.numberLabel) {
            self.numberLabel = [[UILabel alloc] init];
            self.numberLabel.textColor = [UIColor blackColor];
            self.numberLabel.textAlignment = NSTextAlignmentRight;
            self.numberLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14];  ;
        }
        if (!self.priceLabel) {
            self.priceLabel = [[UILabel alloc] init];
            self.priceLabel.textColor = [UIColor blackColor];
            self.priceLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14];
            self.priceLabel.text = @"￥0.00元";
            self.priceLabel.textAlignment = NSTextAlignmentRight;
        }
        [self addSubview:self.nameLabel];
        [self addSubview:self.numberLabel];
        [self addSubview:self.priceLabel];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.priceLabel.frame = CGRectMake(self.width - 100, 0, 90, self.height);
    self.numberLabel.frame = CGRectMake(self.priceLabel.left - 80, 0, 80, self.height);
    self.nameLabel.frame = CGRectMake(14, 0, self.width - self.numberLabel.left - 10, self.height);
}

- (void)updateCell:(NSDictionary *)product{
    if ([product objectForKey:@"product"]) {
        Product *produt = [Product modelWithDictionary:[product objectForKey:@"product"]];
        
        self.nameLabel.text = produt.name;
        
        __weak typeof(self)weakSelf = self;
        [produt.sizes enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([[obj objectForKey:@"_id"] isEqualToString:[product objectForKey:@"selected_size_id"]]) {
                weakSelf.numberLabel.text = @"1件";
                weakSelf.priceLabel.text = [NSString stringWithFormat:@"￥%@",[obj objectForKey:@"lease_price"]];
                *stop = YES;
            }
        }];
    }else{
        Product *produtModel = [Product modelWithDictionary:product];
        
        self.nameLabel.text = produtModel.name;
        self.numberLabel.text = [NSString stringWithFormat:@"%lu件",(unsigned long)produtModel.sizes.count];
        if (produtModel.sizes.count > 0) {
            NSDictionary *obj = produtModel.sizes[0];
            self.priceLabel.text = [NSString stringWithFormat:@"￥%.2f",[[obj objectForKey:@"lease_price"] floatValue]];
        }
    }
}

@end;


#pragma mark - DHOrderDetailLeaseCell

@interface DHOrderDetailLeaseCell()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightLayout;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *raisePriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *freightLabel;
@property (weak, nonatomic) IBOutlet UILabel *preferentialLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalPriceLabel;

@property (strong, nonatomic) NSArray *orderProductArr;

@end

@implementation DHOrderDetailLeaseCell

- (NSArray *)orderProductArr{
    if (!_orderProductArr) {
        _orderProductArr = [NSArray array];
    }
    return _orderProductArr;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[DHOrderDetailLeaseCell_ContentCell class] forCellReuseIdentifier:@"DHOrderDetailLeaseCell_ContentCell"];
}

- (void)updateCellWithProduct:(NSArray *)productArr costDic:(NSArray *)costsArr{
    
    self.orderProductArr = productArr;
    
    if (costsArr) {
        [costsArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([[obj objectForKey:@"cost_title"] isEqualToString:@"租赁费用"]) {
                [self.raisePriceLabel setText:[NSString stringWithFormat:@"租赁费用：￥%.2f",[[obj objectForKey:@"cost_value"] floatValue]]];
                self.raisePriceLabel.hidden = NO;
            }
            if ([[obj objectForKey:@"cost_title"] isEqualToString:@"物流费用"]) {
                [self.freightLabel setText:[NSString stringWithFormat:@"物流费用：￥%.2f",[[obj objectForKey:@"cost_value"] floatValue]]];
                self.freightLabel.hidden = NO;
            }
        }];
    }
    // 计算订单支付金额
    if (costsArr) {
        __block CGFloat payPrice = 0;
        [costsArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CGFloat cost_value = [[obj objectForKey:@"cost_value"] floatValue];
            if (cost_value >= 0) {
                payPrice += cost_value;
            }else{
                payPrice -= cost_value;
            }
        }];
        self.totalPriceLabel.text = [NSString stringWithFormat:@"总额：￥%.2f",payPrice];
    }
    
    self.tableViewHeightLayout.constant = productArr.count * 30;
    
    [self.tableView reloadData];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.orderProductArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHOrderDetailLeaseCell_ContentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DHOrderDetailLeaseCell_ContentCell" forIndexPath:indexPath];
    NSDictionary *product = [self.orderProductArr objectAtIndex:indexPath.row];
    [cell updateCell:product];
    return cell;
}

@end
